//
//  Data+AD.swift
//  ADKit
//
//  Created by Adoma on 2024/04/19.
//

import CommonCrypto

public extension Data {
    
    @available(iOS, deprecated: 13.0)
    func md5() -> Data {
        var result = Data(count: Int(CC_MD5_DIGEST_LENGTH))
        result.withUnsafeMutableBytes { resultPtr in
            self.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
                if let baseAddress = bytes.baseAddress, let _ = bytes.bindMemory(to: UInt8.self).baseAddress {
                    CC_MD5(baseAddress, CC_LONG(self.count), resultPtr.bindMemory(to: UInt8.self).baseAddress)
                }
            }
        }
        return result
    }
    
    func sha1() -> Data {
        var result = Data(count: Int(CC_SHA1_DIGEST_LENGTH))
        result.withUnsafeMutableBytes { resultPtr in
            self.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
                if let baseAddress = bytes.baseAddress, let _ = bytes.bindMemory(to: UInt8.self).baseAddress {
                    CC_SHA1(baseAddress, CC_LONG(self.count), resultPtr.bindMemory(to: UInt8.self).baseAddress)
                }
            }
        }
        return result
    }
}
